package com.ruoyi.ctcx.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ctcx.domain.TbInsuranceRiskContract;
import com.ruoyi.ctcx.service.ITbInsuranceRiskContractService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ctcx.domain.TbInsuranceContractRiskPlan;
import com.ruoyi.ctcx.service.ITbInsuranceContractRiskPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 保险合同风控费用支付计划Controller
 * 
 * @author ruoyi
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/riskContract/plan")
public class TbInsuranceContractRiskPlanController extends BaseController
{
    @Autowired
    private ITbInsuranceContractRiskPlanService tbInsuranceContractRiskPlanService;

    @Autowired
    private ITbInsuranceRiskContractService tbInsuranceRiskContractService;

    /**
     * 查询保险合同风控费用支付计划列表
     */
    @PreAuthorize("@ss.hasPermi('riskContract:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbInsuranceContractRiskPlan tbInsuranceContractRiskPlan)
    {
        startPage();
        List<TbInsuranceContractRiskPlan> list = tbInsuranceContractRiskPlanService.selectTbInsuranceContractRiskPlanList(tbInsuranceContractRiskPlan);
        return getDataTable(list);
    }

    /**
     * 导出保险合同风控费用支付计划列表
     */
    @PreAuthorize("@ss.hasPermi('riskContract:plan:export')")
    @Log(title = "保险合同风控费用支付计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbInsuranceContractRiskPlan tbInsuranceContractRiskPlan)
    {
        List<TbInsuranceContractRiskPlan> list = tbInsuranceContractRiskPlanService.selectTbInsuranceContractRiskPlanList(tbInsuranceContractRiskPlan);
        ExcelUtil<TbInsuranceContractRiskPlan> util = new ExcelUtil<TbInsuranceContractRiskPlan>(TbInsuranceContractRiskPlan.class);
        util.exportExcel(response, list, "保险合同风控费用支付计划数据");
    }

    /**
     * 获取保险合同风控费用支付计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('riskContract:plan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbInsuranceContractRiskPlanService.selectTbInsuranceContractRiskPlanById(id));
    }

    /**
     * 新增保险合同风控费用支付计划
     */
    @PreAuthorize("@ss.hasPermi('riskContract:plan:add')")
    @Log(title = "保险合同风控费用支付计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbInsuranceContractRiskPlan tbInsuranceContractRiskPlan)
    {
        //保存计划
        tbInsuranceContractRiskPlan.setCreateBy(getUsername());
        tbInsuranceContractRiskPlanService.insertTbInsuranceContractRiskPlan(tbInsuranceContractRiskPlan);

        //更新保险合同风控费用
        BigDecimal planAmount = tbInsuranceContractRiskPlanService.getAllPlanAmountByRiskId(tbInsuranceContractRiskPlan.getRiskId());
        TbInsuranceRiskContract tbInsuranceRiskContract =  tbInsuranceRiskContractService.selectTbInsuranceRiskContractById(tbInsuranceContractRiskPlan.getRiskId());
        if (null != tbInsuranceRiskContract)
        {
            tbInsuranceRiskContract.setRiskAmountPlan(planAmount);
            tbInsuranceRiskContractService.updateTbInsuranceRiskContract(tbInsuranceRiskContract);
        }
        return toAjax(1);
    }

    /**
     * 修改保险合同风控费用支付计划
     */
    @PreAuthorize("@ss.hasPermi('riskContract:plan:edit')")
    @Log(title = "保险合同风控费用支付计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbInsuranceContractRiskPlan tbInsuranceContractRiskPlan)
    {
        //保存计划
        tbInsuranceContractRiskPlan.setUpdateBy(getUsername());
        tbInsuranceContractRiskPlanService.updateTbInsuranceContractRiskPlan(tbInsuranceContractRiskPlan);

        //更新保险合同风控费用
        BigDecimal planAmount = tbInsuranceContractRiskPlanService.getAllPlanAmountByRiskId(tbInsuranceContractRiskPlan.getRiskId());
        TbInsuranceRiskContract tbInsuranceRiskContract =  tbInsuranceRiskContractService.selectTbInsuranceRiskContractById(tbInsuranceContractRiskPlan.getRiskId());
        if (null != tbInsuranceRiskContract)
        {
            tbInsuranceRiskContract.setRiskAmountPlan(planAmount);
            tbInsuranceRiskContractService.updateTbInsuranceRiskContract(tbInsuranceRiskContract);
        }

        return toAjax(1);



    }

    /**
     * 删除保险合同风控费用支付计划
     */
    @PreAuthorize("@ss.hasPermi('riskContract:plan:remove')")
    @Log(title = "保险合同风控费用支付计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        for (Long id :ids)
        {

            TbInsuranceContractRiskPlan tbInsuranceContractRiskPlan = tbInsuranceContractRiskPlanService.selectTbInsuranceContractRiskPlanById(id);

            tbInsuranceContractRiskPlanService.deleteTbInsuranceContractRiskPlanById(id);

            //更新保险合同风控费用
            BigDecimal planAmount = tbInsuranceContractRiskPlanService.getAllPlanAmountByRiskId(tbInsuranceContractRiskPlan.getRiskId());
            TbInsuranceRiskContract tbInsuranceRiskContract =  tbInsuranceRiskContractService.selectTbInsuranceRiskContractById(tbInsuranceContractRiskPlan.getRiskId());
            if (null != tbInsuranceRiskContract)
            {
                tbInsuranceRiskContract.setRiskAmountPlan(planAmount);
                tbInsuranceRiskContractService.updateTbInsuranceRiskContract(tbInsuranceRiskContract);
            }
        }
        return toAjax(1);
    }
}
